package com.szmd.gradle.tasks;

import com.android.build.gradle.AppExtension;
import com.android.build.gradle.api.BaseVariant;
import com.android.build.gradle.api.BaseVariantOutput;
import com.google.gson.Gson;
import com.szmd.gradle.Extension;
import com.szmd.gradle.ReleaseHelperPlugin;
import com.szmd.gradle.api.PgyerUploadInterface;
import com.szmd.gradle.api.RetrofitUtil;

import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.tasks.TaskAction;

import java.io.File;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * created by : chentl
 * Date: 2021/01/07
 */
public class PgyerUploadTask extends DefaultTask {

    public BaseVariant baseVariant;
    public Project project;

    public void init(BaseVariant baseVariant, Project project) {
        this.baseVariant = baseVariant;
        this.project = project;
        setDescription("Upload release apk to Pgyer");
        setGroup("release helper");
    }

    @TaskAction
    public void uploadPgyer() {
        AppExtension appExtension = (AppExtension) project.getExtensions().findByName(ReleaseHelperPlugin.ANDROID_EXTENSION_NAME);
        for (BaseVariantOutput it : baseVariant.getOutputs()) {
            File apkFile = it.getOutputFile();
            if (apkFile == null || !apkFile.exists()) {
                throw new GradleException(apkFile + " is not existed!");
            }

            Extension extension = Extension.getConfig(project);
            System.out.println("########################################");
            System.out.println("# applicationId : " + baseVariant.getMergedFlavor().getApplicationId());
            System.out.println("# uploadFileName : " + apkFile.getAbsoluteFile());
            System.out.println("# versionName : " + appExtension.getDefaultConfig().getVersionName());
            System.out.println("# versionCode : " + appExtension.getDefaultConfig().getVersionCode());
            System.out.println("# appName : " + extension.appName);
            System.out.println("# changeLog : " + extension.changeLog);
            System.out.println("########################################");

            try {
                Response<ResponseBody> execute = RetrofitUtil.getRetrofit("https://www.pgyer.com/apiv2/")
                        .create(PgyerUploadInterface.class)
                        .uploadFile(RetrofitUtil.getTextBody(extension.pgyApiKey),
                                RetrofitUtil.getFilePart("application/vnd.android.package-archive", apkFile),
                                RetrofitUtil.getTextBody("2"),
                                RetrofitUtil.getTextBody("123456"),
                                RetrofitUtil.getTextBody("test"),
                                RetrofitUtil.getTextBody("szmd"),
                                RetrofitUtil.getTextBody("2")
                        ).execute();
                System.out.println("\n1. AppsResponse : " + new Gson().toJson(execute.body().toString()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
